package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.service.admin.MarketCouponService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName MarketCouponServiceImpl
 * @Description TODO
 * @Author MZ
 * @Date 2023/5/10 9:05
 */
@Service
public class MarketCouponServiceImpl implements MarketCouponService {

    @Autowired
    MarketCouponMapper couponMapper;
    @Override
    public List<MarketCoupon> list(Integer page, Integer limit, String name, String type, String status, String sort, String order) {

        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = couponExample.or().andDeletedEqualTo(false);
        // 筛选条件name、type、status
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        // page limit 分页使用
        // sort order 排序使用
        couponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketCoupon> marketCouponList = couponMapper.selectByExample(couponExample);

        return marketCouponList;
    }

    @Override
    public void addCoupon(MarketCoupon marketCoupon) {

        marketCoupon.setDeleted(false);
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());

        couponMapper.insert(marketCoupon);

    }


    @Override
    public int update(MarketCoupon marketCoupon) {

        //更新数据
        int resCode = couponMapper.updateByPrimaryKeySelective(marketCoupon);

        return resCode;
    }

    @Override
    public int delete(MarketCoupon marketCoupon) {

        marketCoupon.setDeleted(true);
        //删除数据
        int resCode = couponMapper.updateByPrimaryKeySelective(marketCoupon);

        return resCode;
    }

    @Override
    public List<MarketCoupon> listUser(String page, String limit, String couponId, String sort, String order) {

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        // 逻辑删除为false才显示
        marketCouponExample.or().andDeletedEqualTo(false).andIdEqualTo(Integer.parseInt(couponId));
        marketCouponExample.orderBy(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCoupon> marketCoupons = couponMapper.selectByExampleSelective(marketCouponExample);

        return marketCoupons;
    }

    @Override
    public MarketCoupon read(String id) {

        // 根据id查询
        MarketCoupon coupon = couponMapper.selectByPrimaryKey(Integer.parseInt(id));
        // 关闭session

        return coupon;
    }

}
